﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BonSaiTableAdapters;

public partial class admin_CategoryDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["categoryID"] != null)
            {
                try
                {
                    int categoryID = Int32.Parse(Request.QueryString["categoryID"].ToString());
                    ViewState["categoryID"] = categoryID;
                    LoadCategoryInfo();

                }
                catch (Exception ex)
                {

                }

            }
            else
            {
                btnUpdate.Text = "Tạo mới";
                btnDelete.Visible = false;
                lblHeader.Text = "Thêm mới chủng loại sản phẩm";
            }
        }
    }
    private void LoadCategoryInfo() 
    {
        if (ViewState["categoryID"] != null) 
        {
            int categoryID = Int32.Parse(ViewState["categoryID"].ToString());
            CategoriesTableAdapter categoryDA = new CategoriesTableAdapter();
            BonSai.CategoriesDataTable categoryDT = categoryDA.GetCategoryByID(categoryID);
            if (categoryDT.Rows.Count > 0) 
            {
                BonSai.CategoriesRow categoryRow = categoryDT.Rows[0] as BonSai.CategoriesRow;
                txtCategoryName.Text = categoryRow.CategoryName;
            }
            lblCategoryID.Text = categoryID.ToString();
        }
    }
    
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
            return;

        string categoryName = txtCategoryName.Text;
        CategoriesTableAdapter categoryDA = new CategoriesTableAdapter();



        if (ViewState["categoryID"] != null)
        {
            //Update category
            int categoryID = Int32.Parse(ViewState["categoryID"].ToString());
            categoryDA.Update(categoryName, categoryID);
            Response.Redirect("CategoryManage.aspx?code=2");
        }
        else
        { 
            //Add category
            categoryDA.Insert(categoryName);
            Response.Redirect("CategoryManage.aspx?code=1");
        }
        
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("CategoryManage.aspx");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (ViewState["categoryID"] != null) 
        {
            int categoryID = Int32.Parse(ViewState["categoryID"].ToString());
            ProductsTableAdapter productDA = new ProductsTableAdapter();
            BonSai.ProductsDataTable productDT = productDA.GetProductByCategoryID(categoryID);
            if (productDT.Rows.Count > 0) 
            {
                lblMessage.Text = "Bạn không thể xóa chủng loại này ";
            }
            else
            {
                CategoriesTableAdapter categoryDA = new CategoriesTableAdapter();
                categoryDA.Delete(categoryID);
                Response.Redirect("CategoryManage.aspx?code=3");
            }
        }
    }
}
